package com.nodusk.result;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ParameterUtil {
    public  String notNullAttributes;
    public  String isNullAttributes;
    public String globalSearch;
    public String likeNameSearch;
    public  Double amountSearch;
    public String universalTime;
    public  String multipleChoiceCondition;
    public String fieldConditionStr;

    /**
     * 将 string 值 格式化 写入 Parameter对象中
     * @param parameter
     */
    public void setParameterValue(Parameter parameter){
        if (isNullAttributes != null && !"".equals(isNullAttributes)) {
            String[] split = isNullAttributes.split(",");
            parameter.setIsNullAttributes(split);
        }
        if (multipleChoiceCondition != null && !multipleChoiceCondition.equals("")) {
            Map<String, List<String>> parse = (Map<String, List<String>>) JSONObject.parse(multipleChoiceCondition);
            parameter.setMultipleChoiceCondition(parse);
        }
        if (notNullAttributes != null && !"".equals(notNullAttributes)) {
            String[] split = notNullAttributes.split(",");
            parameter.setNotNullAttributes(split);
        }
        if (likeNameSearch != null && !likeNameSearch.equals("")) {
            Map map = JSONObject.parseObject(likeNameSearch, Map.class);
            parameter.setLikeNameSearch(map);
        }
        if(universalTime!=null&& !universalTime.equals("") ){
            Map<String, Map<String, String>> map= JSONObject.parseObject(universalTime, Map.class);
            parameter.setUniversalTime(map);
        }
        if (globalSearch != null) {
            globalSearch = globalSearch.replaceAll(" ", "");

            String globalSearchNew = "";
            if (!globalSearch.equals("")) {
                String s = globalSearch.replaceAll(" ", ",");
                String[] split = s.split(",");
                List<String> globalSearchList = new ArrayList<>();
                Map<String, String> globalSearchREGEXP = new HashMap<>();
                StringBuffer globalSearchREGEXP7 = new StringBuffer();
                for (String s1 :
                        split) {
                    if (s1 != null && !s1.equals("")) {
                        globalSearchList.add(s1);
                        if (s1.length() > 7) {
                            globalSearchREGEXP7.append(s1 + "|");
                        }
                    }
                }
                if (globalSearchREGEXP7 != null && globalSearchREGEXP7.length() > 2) {
                    globalSearchREGEXP.put("globalSearchREGEXP7", StrUtil.removeSuffix(globalSearchREGEXP7.toString(), "|"));
                }

                parameter.setGlobalSearchREGEXP(globalSearchREGEXP);
                parameter.setGlobalSearchs(globalSearchList);


            }

        }
        if (fieldConditionStr != null) {
            List<FieldCondition> fieldConditionList = JSONArray.parseArray(fieldConditionStr, FieldCondition.class);

            for (FieldCondition fieldCondition : fieldConditionList) {
                String relationId = fieldCondition.getRelationId();
                Object[] datas = fieldCondition.getDatas();
                String databaseId = fieldCondition.getDatabaseId();
                switch (relationId) {
                    case "equal":
                    case "not-equal":
                        if (datas == null || datas.length <= 0 || datas[0].equals("") || datas[0].equals("null")) {
                            continue;
                        }
                            List<FieldCondition> list = parameter.getFieldConditionList();
                            if (list == null) {
                                list = new ArrayList<>();
                            }


                            list.add(fieldCondition);
                            parameter.setFieldConditionList(list);



                        break;
                    case "scope":
                        if (datas == null || datas.length <= 0 ||
                                ((datas[0].equals("") || datas[0].equals("null")) &&
                                        (datas[1].equals("") || datas[1].equals("null"))

                                )) {
                            continue;
                        }
                        Map<String, Map<String, String>> universalTime = parameter.getUniversalTime();
                        if (universalTime == null) {
                            universalTime = new HashMap<>();
                        }
                        Map<String, String> map = new HashMap<>();
                        map.put("startTime", (String) datas[0]);
                        map.put("endTime", (String) datas[1]);
                        universalTime.put(databaseId, map);
                        parameter.setUniversalTime(universalTime);
                        break;
                    case "include":
                        if (datas == null || datas.length <= 0 || datas[0].equals("") || datas[0].equals("null")) {
                            continue;
                        }
                        Map<String, String> likeNameSearchMap = parameter.getLikeNameSearch();
                        if (likeNameSearchMap == null) {
                            likeNameSearchMap = new HashMap<>();
                        }
                        likeNameSearchMap.put(databaseId, (String) datas[0]);
                        parameter.setLikeNameSearch(likeNameSearchMap);
                        break;
                    case "empty":

                        String[] isNullAttributesArr = parameter.getIsNullAttributes();
                        if (isNullAttributesArr == null || isNullAttributesArr.length <= 0) {
                            isNullAttributesArr = new String[]{databaseId};

                        } else {
                            isNullAttributesArr = ArrayUtil.append(isNullAttributesArr, databaseId);
                        }
                        parameter.setIsNullAttributes(isNullAttributesArr);
                        break;
                    case "not-empty":
                        String[] notNullAttributesArr = parameter.getNotNullAttributes();
                        if (notNullAttributesArr == null || notNullAttributesArr.length <= 0) {
                            notNullAttributesArr = new String[]{databaseId};

                        } else {
                            notNullAttributesArr = ArrayUtil.append(notNullAttributesArr, databaseId);
                        }
                        parameter.setNotNullAttributes(notNullAttributesArr);
                        break;

                }
            }


        }
    }
}
